home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-05-23 | 1.6 KB | 62 lines |
- // PrinterDevice
- // Write data to a printer accessible from the machine the JFS server
- // is running on. All printers are listed in the file /etc/printers, and
- // have the format
- //
- // PRINTER = Name ':' TYPE ':' Description ':' Command
- //
- // TYPE = 'Postscript' | 'Text' | 'PPM'
- //
- import java.io.*;
-
- public class PrinterDevice extends DeviceDriver
- {
- // write
- // Valid message parameters are:
- // Printer: <name> // must be in /etc/printers
- void write(Message msg, ServerClient s) throws IOException
- {
- String printer = msg.find("Printer");
- if (printer == null)
- throw new IOException("No Printer given");
-
- // Read /etc/printers
- BufferInputStream pfile = null;
- try pfile = new BufferInputStream(
- FileSystem.getfile("/etc/printers",0,-1,-1));
- catch(BadPathException e)
- throw new IOException("Couldn't read /etc/printers");
-
- // Find the print command for this printer
- String command = null;
- while(true) {
- String line = null;
- try line = pfile.gets();
- catch(IOException e)
- throw new IOException("Printer "+printer+" not found");
- StringSplitter tok = new StringSplitter(line,':');
- if (tok.countTokens() != 4)
- continue; // bogus line
- if (tok.nextToken().equals(printer)) {
- tok.nextToken();
- tok.nextToken();
- command = tok.nextToken();
- break;
- }
- }
-
- // Run the print command, feeding it the data to be printed
- try {
- String cmdarr[] = { "/bin/sh", "-c", command };
- Process proc = Runtime.getRuntime().exec(cmdarr);
- proc.getOutputStream().write(msg.getdata());
- proc.getOutputStream().close();
- proc.waitFor();
- }
- catch(Exception e)
- throw new IOException("Error running print command "+
- command);
- }
- }
-
-